package com.imau.common;

import cn.dev33.satoken.exception.NotLoginException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLIntegrityConstraintViolationException;

/*
 * 全局异常处理
 * @description:
 * @author: fenlo
 * @date: 2022/10/13 12:10
 **/
@ControllerAdvice(annotations = {RestController.class, Controller.class})
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {
    /**
     * 业务异常处理
     *
     * @param ex
     * @return
     */
    @ExceptionHandler(CustomException.class)
    public R customException(CustomException ex) {
        log.error(ex.getMessage());

        return R.error(ResultCodeEnum.GLOBAL_ERROR, ex.getMessage());
    }

    @ExceptionHandler(NotLoginException.class)
    public R handleAccessDeniedException(NotLoginException e) {

        return R.error(ResultCodeEnum.GLOBAL_ERROR, e.getMessage());
    }


}
